package com.lxl.bt.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectChangeListener;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.flyco.tablayout.SlidingTabLayout;
import com.lxl.bt.R;
import com.lxl.bt.fragment.ProductListFragment;
import com.lxl.bt.util.ACache;
import com.lxl.bt.view.CustomViewPager;
import com.yanzhenjie.sofia.Sofia;

import java.util.Arrays;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Create by lvxinlong
 * on  产品列表
 * on  2021年3月5日22:55:32
 **/
public class ProductListActivity extends BaseActivity {


    @BindView(R.id.ivBack)
    ImageView ivBack;
    @BindView(R.id.rlBack)
    RelativeLayout rlBack;
    @BindView(R.id.txt_toolbar_title)
    TextView txtToolbarTitle;
    @BindView(R.id.ivRight)
    ImageView ivRight;
    @BindView(R.id.tvRight)
    TextView tvRight;
    @BindView(R.id.rlRight)
    RelativeLayout rlRight;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tab_layout)
    SlidingTabLayout tabLayout;
    @BindView(R.id.view_pager)
    CustomViewPager viewPager;
    private Context context;
    ACache aCache;
    FragmentAdapter fragmentAdapter;
    private int current;
    private String[] titles = {"算力", "矿机购买"};
    private String[] reqs = { "btc", "eth", "ltc", "fil", ""};
    int index = 0;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_productlist);
        ButterKnife.bind(this);
        //状态栏全透明全屏显示页面，布局能够延伸到状态栏和导航栏下方
        Sofia.with(this)
                .invasionStatusBar()
                .statusBarLightFont()
//                .statusBarDarkFont()
                .statusBarBackgroundAlpha(0)
                .invasionNavigationBar()
                .navigationBarBackgroundAlpha(0);

        context = this;
        rlBack.setVisibility(View.VISIBLE);
        txtToolbarTitle.setText("产品列表");
//        rlRight.setVisibility(View.VISIBLE);
//        tvRight.setText("币种");
        aCache = ACache.get(this);
        current = getIntent().getIntExtra("data", 0);
        refFragmentView();
    }

    void refFragmentView() {
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager());
        viewPager.setAdapter(fragmentAdapter);
        tabLayout.setViewPager(viewPager);
        viewPager.setCurrentItem(current);
        viewPager.setOffscreenPageLimit(titles.length);


        refTableTitleView();
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                current = position;
                refTableTitleView();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setCurrentItem(current);
    }

    private void refTableTitleView() {
        for (int i = 0; i < titles.length; i++) {
            if (i == current) {
                tabLayout.getTitleView(current).getPaint().setFakeBoldText(true);
            } else {
                tabLayout.getTitleView(i).getPaint().setFakeBoldText(false);
            }
        }


    }

    @OnClick({R.id.rlBack, R.id.rlRight})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rlBack:
                finish();
                break;
            case R.id.rlRight:
                OptionsPickerView devtypepvOptions = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        index = options1;
                    }
                })
                        .setTitleText("选择币种")
                        .setContentTextSize(20)//设置滚轮文字大小
                        .setDividerColor(Color.LTGRAY)//设置分割线的颜色
                        .setSelectOptions(0, 0)//默认选中项
                        .setBgColor(R.color.c1E1D1D)
                        .setTitleBgColor(getResources().getColor(R.color.c1E1D1D))
                        .setTitleColor(getResources().getColor(R.color.cffffff))
                        .setCancelColor(getResources().getColor(R.color.colorMain))
                        .setSubmitColor(getResources().getColor(R.color.colorMain))
                        .setTextColorCenter(Color.LTGRAY)
                        .isRestoreItem(true)//切换时是否还原，设置默认选中第一项。
                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                .setLabels("省", "市", "区")
                        .setOutSideColor(0x00000000) //设置外部遮罩颜色
                        .setOptionsSelectChangeListener(new OnOptionsSelectChangeListener() {
                            @Override
                            public void onOptionsSelectChanged(int options1, int options2, int options3) {
//                        String str = "options1: " + options1 + "\noptions2: " + options2 + "\noptions3: " + options3;
//                        Toast.makeText(LoginOrRegestActivity.this, str, Toast.LENGTH_SHORT).show();
                            }
                        })
                        .build();

                devtypepvOptions.setPicker(Arrays.asList(reqs));//一级选择器
                devtypepvOptions.show();
                break;
        }
    }


    private class FragmentAdapter extends FragmentStatePagerAdapter {
        //        private String[] titles;
        public FragmentAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return ProductListFragment.newInstance(titles[position]);
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public void restoreState(Parcelable state, ClassLoader loader) {
        }
    }
}
